# Definition for singly-linked list.
class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None


class Solution:
    def addTwoNumbers(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """

        h1 = l1
        h2 = l2
        l3 = ListNode(0)
        h3 = l3

        step = 0
        d1 = h1.val
        d2 = h2.val
        h3.val = d1 + d2 + step
        if h3.val >= 10:
            step = 1
            h3.val -= 10

        while True:
            h1 = h1.next if h1 is not None else None
            h2 = h2.next if h2 is not None else None
            x1 = 0 if h1 is None else h1.val
            x2 = 0 if h2 is None else h2.val
            if h1 is None and h2 is None and step == 0:
                break

            new_node = ListNode(step)
            h3.next = new_node
            h3 = h3.next
            new_node.val += x1 + x2
            if new_node.val >= 10:
                step = 1
                new_node.val -= 10
            else:
                step = 0

            if h1 is None and h2 is None:
                break

        return l3






